#include "common.hpp"

string cmdPopen(const string& cmdLine) {
	char buffer[1024] = { '\0' };
	FILE* pf = NULL;
	pf = popen(cmdLine.c_str(), "r");
	if (NULL == pf) {
		printf("open pipe failed\n");
		return string("");
	}
	string ret;
	while (fgets(buffer, sizeof(buffer), pf)) {
		ret += buffer;
	}
	pclose(pf);
	return ret;
}

void splitStr(std::string src, std::string pattern,
		std::vector<std::string> &sList) {
	int pos2 = 0;
	//cout << "src       " << src << endl;
	for (int i = 0; i < src.size();) {
		pos2 = src.find('\n', i);
		//cout << "pos2        " << pos2;
		if (pos2 != std::string::npos) {
			std::string strT = src.substr(i, pos2 - i);
			//cout << "    strT        " << strT << endl;
			sList.push_back(strT);
			i = pos2 + 1;
			continue;

		} else {
			sList.push_back(src.substr(i, src.size() - 1));
		}
		break;
	}
}

